package cn.sjtu.mapper;

import cn.sjtu.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author syo 2319615164@qq.com
 * @description UserMapper
 * @date 2024/10/5
 * @Copyright syo
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    // 根据用户名查询用户信息
    @Select("SELECT * FROM t_user WHERE username = #{username}")
    User selectUserByUsername(String username);

    // 根据用户Id查询用户权限
    @Select("SELECT CODE \n" +
            "FROM\n" +
            "\tt_permission \n" +
            "WHERE\n" +
            "\tid IN (\n" +
            "\tSELECT\n" +
            "\t\tpermission_id \n" +
            "\tFROM\n" +
            "\t\tt_role_permission \n" +
            "\tWHERE\n" +
            "\t\trole_id IN ( SELECT id FROM t_role WHERE id IN ( SELECT role_id FROM t_user_role WHERE user_id = #{userId}  ) ) \n" +
            "\t)")
    List<String> selectUserPerm(Long userId);

    // 根据用户Id查询用户角色
    @Select("SELECT role_name FROM t_role WHERE id IN ( SELECT role_id FROM t_user_role WHERE user_id = #{userId})")
    List<String> selectUserRole(Long userId);
}
